package com.fjd.service.impl;

import com.fjd.context.BaseContext;
import com.fjd.dto.OrderSubmitDTO;
import com.fjd.entity.*;
import com.fjd.mapper.*;
import com.fjd.service.ShoppingCartService;
import com.fjd.vo.CartVO;
import com.fjd.vo.GoodVO;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private ShoppingProductMapper shoppingProductMapper;
    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private MerchandiseInfoMapper merchandiseInfoMapper;
    @Autowired
    private DiscountDetailMapper discountDetailMapper;
    @Autowired
    private DiscountMapper discountMapper;


    @Override
    public List<CartVO> list() {
        String customerId = BaseContext.getCurrentId().toString();
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getByCustomerId(customerId);
        List<CartVO> cartVOS = new ArrayList<>();

        for (ShoppingCart sc:shoppingCarts){

            ShoppingProduct shoppingProduct = shoppingProductMapper.getById(sc.getMerchandiseId());

            //获取图片集合的第一张
            String image = productImageMapper.getByProductId(shoppingProduct.getParentId()).get(0);
            MerchandiseInfo merchandiseInfo = merchandiseInfoMapper.getById(shoppingProduct.getParentId());


            CartVO cartVO = new CartVO();
            cartVO.setId(sc.getId());
            cartVO.setProductId(sc.getMerchandiseId());
            cartVO.setSpecification(shoppingProduct.getSpecification());
            cartVO.setAmount(sc.getMerchandiseAmount());
            cartVO.setImageUrl(image);
            cartVO.setPriceTotal(sc.getMerchandisePriceAmount());
            cartVO.setMerchandiseName(merchandiseInfo.getMerchandiseName());


            //加入集合中
            cartVOS.add(cartVO);
        }

        return cartVOS;
    }

    @Override
    @Transactional
    public void add(OrderSubmitDTO orderSubmitDTO) {
        ShoppingProduct shoppingProduct = shoppingProductMapper.getById(orderSubmitDTO.getProductId());
        BigDecimal price = shoppingProduct.getPrice();
        Discount discount = discountMapper.getByMerchandiseIdAndProcessing(shoppingProduct.getParentId());
        if(discount != null){
            price = discountDetailMapper.getDiscountPriceByProductIdAndDiscountId(shoppingProduct.getProductId(),discount.getId()).getDiscountPrice();
        }
        BigDecimal amount = new BigDecimal(orderSubmitDTO.getAmount().toString());
        BigDecimal total = price.multiply(amount);

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .id(UUID.randomUUID().toString().replace("-",""))
                .customerId(BaseContext.getCurrentId().toString())
                .merchandiseAmount(orderSubmitDTO.getAmount())
                .merchandisePrice(price)
                .merchandiseId(shoppingProduct.getProductId())
                .merchandisePriceAmount(total)
                .build();

        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    @Transactional
    public void remove(List<String> ids) {
        shoppingCartMapper.deleteByIds(ids);
    }
}
